package handlers

import (
	"net/http"
	"production-tracker/models"
	"strconv"

	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

type Handler struct {
	db *gorm.DB
}

func NewHandler(db *gorm.DB) *Handler {
	return &Handler{db: db}
}

// WorkPlan handlers
func (h *Handler) GetWorkPlans(c *gin.Context) {
	var workPlans []models.WorkPlan
	h.db.Find(&workPlans)
	c.JSON(http.StatusOK, gin.H{"data": workPlans})
}

func (h *Handler) GetWorkPlan(c *gin.Context) {
	var workPlan models.WorkPlan
	if err := h.db.First(&workPlan, c.Param("id")).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "Work plan not found"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"data": workPlan})
}

func (h *Handler) CreateWorkPlan(c *gin.Context) {
	var workPlan models.WorkPlan
	if err := c.ShouldBindJSON(&workPlan); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	h.db.Create(&workPlan)
	c.JSON(http.StatusCreated, gin.H{"data": workPlan})
}

func (h *Handler) UpdateWorkPlan(c *gin.Context) {
	var workPlan models.WorkPlan
	if err := h.db.First(&workPlan, c.Param("id")).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "Work plan not found"})
		return
	}
	if err := c.ShouldBindJSON(&workPlan); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	h.db.Save(&workPlan)
	c.JSON(http.StatusOK, gin.H{"data": workPlan})
}

func (h *Handler) DeleteWorkPlan(c *gin.Context) {
	var workPlan models.WorkPlan
	if err := h.db.First(&workPlan, c.Param("id")).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "Work plan not found"})
		return
	}
	h.db.Delete(&workPlan)
	c.JSON(http.StatusOK, gin.H{"message": "Work plan deleted"})
}

// Material handlers
func (h *Handler) GetMaterials(c *gin.Context) {
	var materials []models.Material
	h.db.Find(&materials)
	c.JSON(http.StatusOK, gin.H{"data": materials})
}

func (h *Handler) GetMaterial(c *gin.Context) {
	var material models.Material
	if err := h.db.First(&material, c.Param("id")).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "Material not found"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"data": material})
}

func (h *Handler) CreateMaterial(c *gin.Context) {
	var material models.Material
	if err := c.ShouldBindJSON(&material); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	h.db.Create(&material)
	c.JSON(http.StatusCreated, gin.H{"data": material})
}

func (h *Handler) UpdateMaterial(c *gin.Context) {
	var material models.Material
	if err := h.db.First(&material, c.Param("id")).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "Material not found"})
		return
	}
	if err := c.ShouldBindJSON(&material); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	h.db.Save(&material)
	c.JSON(http.StatusOK, gin.H{"data": material})
}

func (h *Handler) DeleteMaterial(c *gin.Context) {
	var material models.Material
	if err := h.db.First(&material, c.Param("id")).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "Material not found"})
		return
	}
	h.db.Delete(&material)
	c.JSON(http.StatusOK, gin.H{"message": "Material deleted"})
}

// WorkOrderData handlers
func (h *Handler) GetWorkOrderData(c *gin.Context) {
	var workOrderData []models.WorkOrderData
	h.db.Find(&workOrderData)
	c.JSON(http.StatusOK, gin.H{"data": workOrderData})
}

func (h *Handler) GetWorkOrderDatum(c *gin.Context) {
	var workOrderDatum models.WorkOrderData
	if err := h.db.First(&workOrderDatum, c.Param("id")).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "Work order data not found"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"data": workOrderDatum})
}

func (h *Handler) CreateWorkOrderDatum(c *gin.Context) {
	var workOrderDatum models.WorkOrderData
	if err := c.ShouldBindJSON(&workOrderDatum); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	h.db.Create(&workOrderDatum)
	c.JSON(http.StatusCreated, gin.H{"data": workOrderDatum})
}

func (h *Handler) UpdateWorkOrderDatum(c *gin.Context) {
	var workOrderDatum models.WorkOrderData
	if err := h.db.First(&workOrderDatum, c.Param("id")).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "Work order data not found"})
		return
	}
	if err := c.ShouldBindJSON(&workOrderDatum); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	h.db.Save(&workOrderDatum)
	c.JSON(http.StatusOK, gin.H{"data": workOrderDatum})
}

func (h *Handler) DeleteWorkOrderDatum(c *gin.Context) {
	var workOrderDatum models.WorkOrderData
	if err := h.db.First(&workOrderDatum, c.Param("id")).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "Work order data not found"})
		return
	}
	h.db.Delete(&workOrderDatum)
	c.JSON(http.StatusOK, gin.H{"message": "Work order data deleted"})
}

// Stencil handlers
func (h *Handler) GetStencils(c *gin.Context) {
	var stencils []models.Stencil
	h.db.Find(&stencils)
	c.JSON(http.StatusOK, gin.H{"data": stencils})
}

func (h *Handler) GetStencil(c *gin.Context) {
	var stencil models.Stencil
	if err := h.db.First(&stencil, c.Param("id")).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "Stencil not found"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"data": stencil})
}

func (h *Handler) CreateStencil(c *gin.Context) {
	var stencil models.Stencil
	if err := c.ShouldBindJSON(&stencil); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	h.db.Create(&stencil)
	c.JSON(http.StatusCreated, gin.H{"data": stencil})
}

func (h *Handler) UpdateStencil(c *gin.Context) {
	var stencil models.Stencil
	if err := h.db.First(&stencil, c.Param("id")).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "Stencil not found"})
		return
	}
	if err := c.ShouldBindJSON(&stencil); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	h.db.Save(&stencil)
	c.JSON(http.StatusOK, gin.H{"data": stencil})
}

func (h *Handler) DeleteStencil(c *gin.Context) {
	var stencil models.Stencil
	if err := h.db.First(&stencil, c.Param("id")).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "Stencil not found"})
		return
	}
	h.db.Delete(&stencil)
	c.JSON(http.StatusOK, gin.H{"message": "Stencil deleted"})
}

// Fixture handlers
func (h *Handler) GetFixtures(c *gin.Context) {
	var fixtures []models.Fixture
	h.db.Find(&fixtures)
	c.JSON(http.StatusOK, gin.H{"data": fixtures})
}

func (h *Handler) GetFixture(c *gin.Context) {
	var fixture models.Fixture
	if err := h.db.First(&fixture, c.Param("id")).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "Fixture not found"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"data": fixture})
}

func (h *Handler) CreateFixture(c *gin.Context) {
	var fixture models.Fixture
	if err := c.ShouldBindJSON(&fixture); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	h.db.Create(&fixture)
	c.JSON(http.StatusCreated, gin.H{"data": fixture})
}

func (h *Handler) UpdateFixture(c *gin.Context) {
	var fixture models.Fixture
	if err := h.db.First(&fixture, c.Param("id")).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "Fixture not found"})
		return
	}
	if err := c.ShouldBindJSON(&fixture); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	h.db.Save(&fixture)
	c.JSON(http.StatusOK, gin.H{"data": fixture})
}

func (h *Handler) DeleteFixture(c *gin.Context) {
	var fixture models.Fixture
	if err := h.db.First(&fixture, c.Param("id")).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "Fixture not found"})
		return
	}
	h.db.Delete(&fixture)
	c.JSON(http.StatusOK, gin.H{"message": "Fixture deleted"})
}

// SOPApproval handlers
func (h *Handler) GetSOPApprovals(c *gin.Context) {
	var sopApprovals []models.SOPApproval
	h.db.Find(&sopApprovals)
	c.JSON(http.StatusOK, gin.H{"data": sopApprovals})
}

func (h *Handler) GetSOPApproval(c *gin.Context) {
	var sopApproval models.SOPApproval
	if err := h.db.First(&sopApproval, c.Param("id")).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "SOP approval not found"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"data": sopApproval})
}

func (h *Handler) CreateSOPApproval(c *gin.Context) {
	var sopApproval models.SOPApproval
	if err := c.ShouldBindJSON(&sopApproval); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	h.db.Create(&sopApproval)
	c.JSON(http.StatusCreated, gin.H{"data": sopApproval})
}

func (h *Handler) UpdateSOPApproval(c *gin.Context) {
	var sopApproval models.SOPApproval
	if err := h.db.First(&sopApproval, c.Param("id")).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "SOP approval not found"})
		return
	}
	if err := c.ShouldBindJSON(&sopApproval); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	h.db.Save(&sopApproval)
	c.JSON(http.StatusOK, gin.H{"data": sopApproval})
}

func (h *Handler) DeleteSOPApproval(c *gin.Context) {
	var sopApproval models.SOPApproval
	if err := h.db.First(&sopApproval, c.Param("id")).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "SOP approval not found"})
		return
	}
	h.db.Delete(&sopApproval)
	c.JSON(http.StatusOK, gin.H{"message": "SOP approval deleted"})
}

// Bulk operations
func (h *Handler) ExportData(c *gin.Context) {
	// 导出数据逻辑
	c.JSON(http.StatusOK, gin.H{"message": "Export functionality to be implemented"})
}

func (h *Handler) ImportData(c *gin.Context) {
	// 导入数据逻辑
	c.JSON(http.StatusOK, gin.H{"message": "Import functionality to be implemented"})
}
